<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 50px;
            height: 50px;
            position: absolute;
            left: 0px;
            top: 0px;
            background-color: #09f;
            opacity: 0.5;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div id="demo"></div>

    <script>
        var oDiv = document.getElementById('demo');
        // oDiv.onclick = function() {
        //     startMove(this);
        // }

        var speedX = 0;
        var speedY = 0;
        var previousX = 0;
        var previousY = 0;
        oDiv.onmousedown = function(e) {
            clearInterval(this.timer);
            var event = e || window.event;
            // console.log(event);
            var disX = event.pageX - this.offsetLeft;
            var disY = event.pageY - this.offsetTop;
            var that = this;
            document.onmousemove = function(e) {
                var event = e || window.event;
                // console.log(event);
                var newLeft = event.pageX - disX;
                var newTop = event.pageY - disY;

                speedX = newLeft - previousX;
                speedY = newTop - previousY;
                previousX = newLeft;
                previousY = newTop;
                // 浏览器对onmousemove事件的监控也是有一定时间间隔的，每隔一定时间(时间间隔不变)触发一次

                // var span = document.createElement('span');
                // span.style.position = 'absolute';
                // span.style.width = '5px';
                // span.style.height = '5px';
                // span.style.backgroundColor = '#f40';
                // span.style.left = newLeft + 'px';
                // span.style.top = newTop + 'px';
                // document.body.appendChild(span);



                that.style.left = newLeft + 'px';
                that.style.top = newTop + 'px';
            }
            document.onmouseup = function() {
                document.onmousemove = null;
                document.onmouseup = null; 
                startMove(that, speedX, speedY);
            }
        }


        function startMove(dom, speedX, speedY) {
            clearInterval(dom.timer);
            var g = 3;
            dom.timer = setInterval(function(){
                speedY = speedY + g;
                var newLeft = dom.offsetLeft + speedX;
                var newTop = dom.offsetTop + speedY;
                if(newTop + dom.offsetHeight >= window.innerHeight) {
                    newTop = window.innerHeight - dom.offsetHeight;
                    speedY = -speedY;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newLeft + dom.offsetWidth >= window.innerWidth){
                    newLeft = window.innerWidth - dom.offsetWidth;
                    speedX = - speedX;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newTop <= 0) {
                    newTop = 0;
                    speedY = -speedY;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                if(newLeft <= 0) {
                    newLeft = 0;
                    speedX = - speedX;
                    speedY *= 0.8
                    speedX *= 0.8
                }
                // console.log(speedX, speedY);
                if(Math.abs(speedX) < 1) {
                    speedX = 0;
                }
                if(Math.abs(speedY) < 1) {
                    speedY = 0;
                }
                if(speedX == 0 && speedY == 0 && newTop == window.innerHeight - dom.offsetHeight) {
                    clearInterval(dom.timer);
                    console.log('over');
                }else{
                    dom.style.left = newLeft + 'px';
                    dom.style.top = newTop + 'px';
                }
            }, 30);
        }
    </script>
</body>
</html>